import { GraphQLScalarType, Kind } from 'graphql'

export const typeDefs = `
  scalar Date

  type userProject {
    _id : ID
    id  : String
    ref : Int
  }

  type userTeams {
    _id : ID
    avatar : String
    name : String
  }

  type userOrgs {
    _id  : ID
    role : String
    avatar : String
    name : String
  }

  type User {
    _id: ID
    updatedAt : Date
    createdAt : Date
    name : String
    type: String
    avatar : String
    bio : String
    email : String
    status : String
    projects : [userProject]
    teams: [userTeams]
    orgs: [userOrgs]
    star_number : Int
    issue_number : Int
    message_number : Int
    project_number : Int
    team_number : Int
  }

  type projectPartner {
    role: String
    status: String
    _id: ID
    avatar: String
    name: String
  }

  type projectTeam {
    role: String
    status: String
    _id: ID
    avatar: String
    name: String
  }

  type KeyPointPoint {
    name: String
    coordinate: [Float]
    color: String
  }

  type ProjectClasses {
    color: String
    type: String
    bio: String
    name: String
    points: [KeyPointPoint]
    lines: [[Float]]
    _id: ID
  }

  type Project {
    _id: ID
    name: String
    updatedAt: Date
    createdAt: Date
    bio: String
    owner: String
    sourceType: String
    items: Int
    users: Int
    stars: Int
    partners: [projectPartner]
    teams: [projectTeam]
    classes: [ProjectClasses]
  }

  type ProjectLabelView {
    id: ID
    name: String
    unfinished: Int
    finished: Int
    reopend: Int
    classes: [ProjectClasses]
  }

  type ImageItemAnnotation{
    id : ID
    description : String
    class_id : String
    interior : [[[Float]]]
    exterior : [[Float]]
    tags : [String]
  }

  type ImageItemReview {
    _id : ID
  }

  type ImageItemJudge {
    _id : ID
  }

  type ImageItemVersion {
    _id: ID
    createdAt : Date
    from: String
    assign : String
    type: String
    label: ImageItemAnnotation
    review: String
    judge: String
  }

  type Image {
    _id : ID
    width : Int
    height : Int
    md5 : String
    path : String
    thumbnailPath : String
    size : Int,
  }

  type ImageItem {
    id : ID
    image : Image
    label: [ImageItemAnnotation]
  }

  type ImageItemInfo {
    itemId: ID
    name: String
    state: String
    thumb: String
  }

  type Post {
    id: Int
    title: String
    views: Int
    author: User
  }

  interface Message {
    content: String
  }
  type Notice implements Message {
    content: String
    noticeTime: Date
  }
  type Remind implements Message {
    content: String
    endTime: Date
  }

  type Query {
    aString: String
    aBoolean: Boolean
    anInt: Int
    user: User
    projectLabelView(id: ID): ProjectLabelView
    itemsPage(id: ID): [ImageItemInfo]
    author(id: Int): User
    topPosts(limit: Int): [Post]
    notices: [Notice]
    itemInfo(id: ID): ImageItem
  }

  type Mutation {
    addUser: User
    newLabel(title: String, points: [[Float]] ): ImageItemAnnotation
  }
`

export const typeResolvers = {
  Message: {
    __resolveType: data => data.typename,
  },
  Date: new GraphQLScalarType({
    name: 'Date',
    description: 'Date custom scalar type',
    parseValue(value) {
      return new Date(value)
    },
    serialize(value) {
      return new Date(value)
    },
    parseLiteral(ast) {
      if (ast.kind === Kind.INT) {
        return parseInt(ast.value, 10)
      }
      return null
    },
  }),
}
